package com.wanchuang.hepos.ui.page.main;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.ViewModelProviders;

import com.wanchuang.hepos.R;
import com.wanchuang.hepos.bridge.data.bean.ProgressBean;
import com.wanchuang.hepos.bridge.request.CountRequestViewModel;
import com.wanchuang.hepos.bridge.state.main.CountFragmentViewModel;
import com.wanchuang.hepos.databinding.FragmentCountBinding;
import com.wanchuang.hepos.proto.Statistics;
import com.wanchuang.hepos.ui.base.BaseFragment;
import com.wanchuang.hepos.ui.dialog.SelectMonthDialog;
import com.wanchuang.hepos.ui.view.FullyGridLayoutManager;

import java.util.List;


/**
 * 文件名 : HomeRootFragment
 * 创建者 : MangoWang
 * 创建日期 : 2019/12/24 16:20
 * 微信 : MangoWaWang
 * 邮箱 : 763482205@qq.com
 * 描述 : TODO
 */
public class CountFragment extends BaseFragment {

    private FragmentCountBinding mBinding;
    private CountFragmentViewModel mViewModel;
    private CountRequestViewModel mRequestViewModel;


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mViewModel = ViewModelProviders.of(this).get(CountFragmentViewModel.class);
        mRequestViewModel = ViewModelProviders.of(this).get(CountRequestViewModel.class);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_count, container, false);
        mBinding = FragmentCountBinding.bind(view);
        mBinding.setClick(new ClickProxy());
        // viewModel不应持有content
        mViewModel.layoutManager.set(new FullyGridLayoutManager(mActivity, 3));
        mBinding.setVm(mViewModel);
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        mRequestViewModel.getErrorString().observe(this, s -> {
            hideProgress();
            showShortToast(s);
        });

        mRequestViewModel.getResultString().observe(this, s -> {
            hideProgress();
            showShortToast(s);
        });

        // 加载上页表 和 下面滚轮数据 处理
        mRequestViewModel.getStatists().observe(this, get_statistics -> {
            mViewModel.money.set(get_statistics.getTotalAmount());
            mViewModel.fee.set("总扣除手续费￥" + get_statistics.getFee() + " 实际到账￥" + get_statistics.getSettleAmount());
            List<Statistics.statistics> listList = get_statistics.getListList();
            mViewModel.data.clear();
            double total = Double.valueOf(get_statistics.getTotalAmount());
            // 逻辑处理本应写在adapter，但是total在数据最外层 ，无奈之下这样处理
            for (Statistics.statistics bean : listList) {
                ProgressBean progressBean = new ProgressBean();
                progressBean.setName(bean.getTime());
                double amount = Double.valueOf(bean.getAmount());
                double percent = amount / total * 100;
                if (0 < percent && percent < 1) {
                    percent = 1;
                }
                progressBean.setProgess((int) percent);
                progressBean.setPercent(String.valueOf((int) percent));
                mViewModel.data.add(progressBean);
            }
            mViewModel.adapter.get().notifyDataSetChanged();
            mViewModel.adapter.get().getLoadMoreModule().loadMoreEnd(true);
        });

        // 图标数据 处理
        mRequestViewModel.getTrend().observe(this, get_statistics -> {
            hideProgress();
            // 少用这中方式设置数据
            mBinding.chart.setLineData(get_statistics.getListList());
        });

        // 默认加载数据
        loadData();

    }

    /**
     * 加载数据
     */
    private void loadData() {
        String time = mViewModel.year.get() + "-" + mViewModel.month.get();
        mRequestViewModel.get_statistics(time);
        mRequestViewModel.get_trend(time);

    }

    /**
     * 点击代理
     */
    public class ClickProxy {
        public void back() {
        }
        public void selectMonth() {
            if (mViewModel.build == null) {
                SelectMonthDialog.Builder builder = new SelectMonthDialog.Builder(mActivity);
                builder.setYearAndMonth(Integer.valueOf(mViewModel.year.get()), Integer.valueOf(mViewModel.month.get()))
                        .setOnDialogClickListener((view, month) -> {
                            String split[] = month.split("-");
                            mViewModel.year.set(split[0]);
                            mViewModel.month.set(split[1]);
                            mViewModel.time.set(split[0] + "年" + split[1] + "月");
                            showProgress();
                            loadData();
                        });
                mViewModel.build = builder.build();
                mViewModel.build.setCancelable(true);
            }            // 选择月份
            mViewModel.build.show(getChildFragmentManager());
        }

        public void itemOnClick(int position) {
        }

        public void loadMoreListener() {
        }
    }


}
